package com.leo.controller;

import com.leo.model.UserInfo;
import com.leo.service.UserInfoService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HelloController
 * @Description: ${description}
 * @Author: leo825
 * @Date: 2019-12-31 17:01
 * @Version: 1.0
 */

@Controller
public class HelloController {

    //获取日志
    private static final Logger LOGGER = LogManager.getLogger();

    @Autowired
    UserInfoService userInfoService;

    /**
     * 返回一个ModeAndView
     *
     * @return
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ModelAndView hello() {
        LOGGER.info("使用配置实现 hello controller 跳转到 success");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("data", "恭喜您，测试成功了");
        modelAndView.setViewName("success");//跳转到/WEB-INF/views/success.jsp
        return modelAndView;
    }

    /**
     * 使用Model绑定数据
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/helloModel", method = RequestMethod.GET)
    public String helloModel(Model model) {
        model.addAttribute("data", "使用Model绑定数据");
        return "success";
    }

    /**
     * 使用Model绑定数据
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/helloModelMap", method = RequestMethod.GET)
    public String helloModelMap(ModelMap modelMap) {
        modelMap.addAttribute("data", "使用ModelMap绑定数据");
        return "success";
    }

    /**
     * 返回逻辑视图“success”
     *
     * @return
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    @RequestMapping(value = "/hello2", method = RequestMethod.GET)
    public String hello2() {
        LOGGER.info("访问了 hello2");
        return "success";
    }

    /**
     * 返回一个字符串
     *
     * @return
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    @RequestMapping(value = "/hello3", method = RequestMethod.GET)
    @ResponseBody
    public String hello3() {
        LOGGER.info("访问了 hello3");
        String hello = "你好 SpringMVC";
        return hello;
    }


    @RequestMapping(value = "/helloUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public Map helloUserInfo(){
        Map<String, Object> map = new HashMap<>();
        map.put("name", "leo");
        map.put("sex", "男");
        return map;
    }

    @RequestMapping(value = "/getUserInfoList", method = RequestMethod.GET)
    @ResponseBody
    public String getUserInfoList(HttpServletRequest request){
        LOGGER.info("获取参数：" + request.getParameter("ipAddr"));
        List<UserInfo> list = userInfoService.getUserInfoList();
        LOGGER.info(list.toString());
        return list.toString();
    }

    @RequestMapping(value = "/getIpAddr", method = RequestMethod.GET)
    @ResponseBody
    public String getIpAddr(HttpServletRequest request){
        String ipAddr = request.getParameter("ipAddr");
        LOGGER.info("获取参数：" + ipAddr);
        return ipAddr;
    }
}